#coding:utf-8
import json
from lovewith.account.views.modules.userapi import UserApi
from lovewith.api.weibo_tools import Weibo
from lovewith.api.qq_tools import QQ


#调度新浪&&腾讯微博发送消息
class WbControlCenter:
    def __init__(self, user, sync_type=None):
        #同步类型
        self.sync_type = sync_type
        #用户
        self.user = user
        self.sync_wb = {
            'token': None,
            'expires': None,
            'config': None
        }
        self.sync_qq = {
            'token': None,
            'expires': None,
            'config': None
        }

        #已登录并且非管理员
        if user.is_authenticated() and not user.is_admin:
            auth_data = UserApi.get_auth(user.id)
            for au in auth_data:
                au_data = auth_data[au]
                #判断是否过期
                if not au_data.get('is_expires'):
                    #保存同步设置
                    if au_data.get('type') == 1:
                        self.sync_wb['token'] = au_data.get('token')
                        self.sync_wb['expires'] = au_data.get('expires')
                        self.sync_wb['sync'] = au_data.get('sync')
                    if au_data.get('type') == 2:
                        self.sync_qq['token'] = au_data.get('token')
                        self.sync_qq['expires'] = au_data.get('expires')
                        self.sync_qq['sync'] = au_data.get('sync')

    #获取用户设置的同步状态
    def get_sync_state(self, account=None, sync_type=None):
        if account and sync_type:
            sync_data = (account == 'wb') and self.sync_wb or self.sync_qq
            if sync_data:
                sync_state = sync_data['sync'][sync_type]
                if sync_state == '0':
                    return False

        return True

    #关注
    def follow_us(self, sync_wb=False, sync_qq=False):
        if self.sync_wb.get('token'):
            if self.get_sync_state('wb', self.sync_type) or sync_wb:
                Weibo(self.sync_wb.get('token')).follow(2616158115)

        if self.sync_qq.get('token'):
            if self.get_sync_state('qq', self.sync_type) or sync_qq:
                qq = QQ()
                qq.save_token(self.sync_qq.get('token'), self.sync_qq.get('expires'))
                qq.follow('lovewith-me')

    #替换微博正文中@的用户名
    @staticmethod
    def replace_post_text(text, at_user_id):
        wb_username = UserApi.get_auth_username(at_user_id, 1)
        qq_username = UserApi.get_auth_username(at_user_id, 2)

        if at_user_id and wb_username:
            wb_text = text.replace('@_@', '@%s' % wb_username)
        else:
            wb_text = text.replace('@_@', '')

        if at_user_id and qq_username:
            qq_text = text.replace('@_@', '@%s' % qq_username)
        else:
            qq_text = text.replace('@_@', '')

        return {
            'wb': wb_text,
            'qq': qq_text
        }

    #发送文字文博
    def send_text(self, text, at_user_id=None, sync_wb=False, sync_qq=False):
        post_text = self.replace_post_text(text, at_user_id)

        if self.sync_wb.get('token'):
            if self.get_sync_state('wb', self.sync_type) or sync_wb:
                Weibo(self.sync_wb.get('token')).post_text(post_text['wb'])

        if self.sync_qq.get('token'):
            if self.get_sync_state('qq', self.sync_type) or sync_qq:
                qq = QQ()
                qq.save_token(self.sync_qq.get('token'), self.sync_qq.get('expires'))
                qq.post_text(post_text['qq'])

    #发送图片微博
    def send_media(self, text, pic, at_user_id=None, sync_wb=False, sync_qq=False):
        post_text = self.replace_post_text(text, at_user_id)

        if self.sync_wb.get('token'):
            if self.get_sync_state('wb', self.sync_type) or sync_wb:
                Weibo(self.sync_wb.get('token')).post_with_pic(post_text['wb'], pic)

        if self.sync_qq.get('token'):
            if self.get_sync_state('qq', self.sync_type) or sync_qq:
                qq = QQ()
                qq.save_token(self.sync_qq.get('token'), self.sync_qq.get('expires'))
                qq.post_with_pic(post_text['qq'], pic)